package com.yanqu.road.dao.impl.activity.summergames;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.summergames.cross.CrossSummerGamesExchangeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class CrossSummerGamesExchangeDataDaoImpl extends TempDataObjectDao<CrossSummerGamesExchangeData> {


    public long getMaxExchangeId(int activityId){
        String sql = "select ifnull(max(id),0) as maxId from t_c_summer_games_exchange_data where activityId = ?";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

    public static String getCrossSummerGamesExchangeDataSql = "select * from `t_c_summer_games_exchange_data` where `activityId`=? and (`status` = 0 or `status` = 1)";
    public List<CrossSummerGamesExchangeData> getCrossSummerGamesExchangeData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossSummerGamesExchangeData> list = new LinkedList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossSummerGamesExchangeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossSummerGamesExchangeData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateCrossSummerGamesExchangeDataSql = "update `t_c_summer_games_exchange_data` set pUserId=?,pServerId=?,pGoodsId=?,wantGoodsId=?,unionUid=?,aUserId=?,aServerId=?,status=?,pTime=?,applyType=?,lastUpdateTime=?,nickName=? where activityId=? and id=?";
    @Override
    protected boolean updateData(CrossSummerGamesExchangeData crossSummerGamesExchangeData) {
        crossSummerGamesExchangeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getPUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getPServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesExchangeData.getPGoodsId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesExchangeData.getWantGoodsId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSummerGamesExchangeData.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getAUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getAServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesExchangeData.getStatus()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getPTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesExchangeData.getApplyType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSummerGamesExchangeData.getNickName()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesExchangeData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getId()));
        if (getDb().executeNoneQuery(updateCrossSummerGamesExchangeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSummerGamesExchangeDataSql = "insert into `t_c_summer_games_exchange_data`(activityId,id,pUserId,pServerId,pGoodsId,wantGoodsId,unionUid,aUserId,aServerId,status,pTime,applyType,lastUpdateTime,nickName) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossSummerGamesExchangeData crossSummerGamesExchangeData) {
        crossSummerGamesExchangeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesExchangeData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getPUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getPServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesExchangeData.getPGoodsId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesExchangeData.getWantGoodsId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSummerGamesExchangeData.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getAUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getAServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesExchangeData.getStatus()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getPTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesExchangeData.getApplyType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesExchangeData.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSummerGamesExchangeData.getNickName()));
        if (getDb().executeNoneQuery(insertCrossSummerGamesExchangeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSummerGamesExchangeData parseData(ResultSet rs) throws SQLException {
        CrossSummerGamesExchangeData crossSummerGamesExchangeData = new CrossSummerGamesExchangeData();
        crossSummerGamesExchangeData.setActivityId(rs.getInt("activityId"));
        crossSummerGamesExchangeData.setId(rs.getLong("id"));
        crossSummerGamesExchangeData.setPUserId(rs.getLong("pUserId"));
        crossSummerGamesExchangeData.setPServerId(rs.getLong("pServerId"));
        crossSummerGamesExchangeData.setPGoodsId(rs.getInt("pGoodsId"));
        crossSummerGamesExchangeData.setWantGoodsId(rs.getInt("wantGoodsId"));
        crossSummerGamesExchangeData.setUnionUid(rs.getString("unionUid"));
        crossSummerGamesExchangeData.setAUserId(rs.getLong("aUserId"));
        crossSummerGamesExchangeData.setAServerId(rs.getLong("aServerId"));
        crossSummerGamesExchangeData.setStatus(rs.getInt("status"));
        crossSummerGamesExchangeData.setPTime(rs.getLong("pTime"));
        crossSummerGamesExchangeData.setApplyType(rs.getInt("applyType"));
        crossSummerGamesExchangeData.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        crossSummerGamesExchangeData.setNickName(rs.getString("nickName"));
        return crossSummerGamesExchangeData;
    }
}
